import { jobGoto } from "./goto";
import { jobSpread } from "./spread";

export const jobBuild={
	/**
	 * 
	 * @param {Creep} creep 
	 * @param {RoomObject} targets
	 */
    run(creep, targets=undefined){
		if(targets===undefined){
			targets = creep.room.find(FIND_CONSTRUCTION_SITES, {
				filter(site){
					return site.my;
				}
			});
			targets=jobGoto.sortTargetsByDistance(creep, targets);
		}
		let ret;
		let finalTarget;
		for(let target of targets){
			ret = creep.build(target);
			if(ret === OK || ret === ERR_NOT_IN_RANGE){
				finalTarget = target;
				if(ret === OK){
					break;
				}
			}
		}
		if(!finalTarget){
			return jobSpread.run(creep);
			// return OK;
		}
		if(ret === ERR_NOT_IN_RANGE || !creep.pos.isNearTo(finalTarget)) {
			// ret = jobGoto.run(creep, [finalTarget])
			jobGoto.run(creep, targets, {visualizePathStyle: {stroke: '#ffffff'}});
		}
		return ret;
    }
}